package copyengine.dataLayer.rpc.moudle.codeConvert
{
	import copyengine.utils.debug.CELog;

	public class CEAMFRpcCodeConvert implements ICERpcCodeConvertModule
	{
		/**
		 *define which protoco that current system use . this Version number will hardcode.
		 */
		private static const RPC_PROTOCOL_VERSION:String = "0.0.1";

		/**
		 *secretKey will initialze by first request result ,
		 * first requst server will pass the secret back , then after first request each reqeust
		 * need take the serrectKey with , server will use this key to judge is user open two brownser
		 * or not
		 */
		private var secretKey:String;

		private var token:String;

		public function CEAMFRpcCodeConvert()
		{
			secretKey = "not initialze";
		}

		public function setSecretKey(_key:String):void
		{
			secretKey = _key;
		}

		public function setToken(_token:String):void
		{
			token = _token;
		}

		public function logicLayerDataToTransferLayerData(_logicLayerDataArray:Array, _requestCount:int):*
		{
			var data:Object = new Object();
			var header:Object = {version:RPC_PROTOCOL_VERSION, sk:secretKey, st:_requestCount, token:token};
			data["header"] = header;
			data["data"] = _logicLayerDataArray;
			return data;
		}

		public function transferLayerDataToLogicLayerData(_transferLayerData:*):Array
		{
			try
			{
				var result:Object = _transferLayerData;
				if (result["header"]["errCode"] > 0)
				{
					CELog.err("GlobalError Code : " + result["header"]["errCode"] + " request data reject to read");
					return null;
				}
				return result["data"];
			}
			catch (e:Error)
			{
				CELog.err("Receive data not use right format ,data reject read");
				return null;
			}
			return null;
		}
	}
}
